#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/1/26 14:36
# @Author  : Ramsey
# @Site    : zh
# @File    : scheduler.py
# @Software: PyCharm
from typing import Optional
from spider_framework.utils.priority_queue import SpiderPriorityQueue


class Scheduler:
    def __init__(self, crawler):
        self.crawler = crawler
        self.request_queue: Optional[SpiderPriorityQueue] = None

    def open(self):
        self.request_queue = SpiderPriorityQueue()

    async def next_request(self):
        return await self.request_queue.get()

    async def push_request(self, request):
        self.crawler.status_collector.increase_value(key="request_scheduler_count", count=1)
        await self.request_queue.put(request)

    def idea(self):
        return len(self) == 0

    def __len__(self):
        return self.request_queue.qsize()


if __name__ == "__main__":
    pass
